<script setup lang="ts">
import CustomNavbar from '@/pages/index/components/CustomNavbar.vue'
import CategoryPanel from '@/pages/index/components/CategoryPanel.vue'
import HotPanel from '@/pages/index/components/HotPanel.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
// import type { XtxGuessInstance } from '@/types/component'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import PageSkeleton from '@/pages/index/components/PageSkeleton.vue'
import { useGuessList } from '@/composables'

// 在 pages.json 中配置了自动导入规则，所以不用手动导入也可以使用 XtxSwiper
// import XtxSwiper from '@/components/XtxSwiper.vue'

/**
 * 轮播图广告数组
 */
const bannerList = ref<BannerItem[]>([])
/**
 * 获取首页轮播图数据
 */
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  // console.log(res)
  bannerList.value = res.result
}

/**
 * 前台分类数据数组
 */
const categoryList = ref<CategoryItem[]>([])
/**
 * 获取首页前台分类数据
 */
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  // console.log(res)
  categoryList.value = res.result
}

/**
 * 热门推荐数据数组
 */
const hotList = ref<HotItem[]>([])
/**
 * 获取首页热门推荐数据
 */
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

/**
 * 是否加载中的标记
 */
const isLoading = ref(false)

/**
 * 页面加载钩子
 */
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isLoading.value = false
})

// /**
//  * 获取猜你喜欢组件实例
//  */
// const guessRef = ref<XtxGuessInstance>()
// /**
//  * 滚动容器滚动到底部/右边，触发 onScrolltolower 事件
//  */
// const onScrolltolower = () => {
//   guessRef.value?.getMore()
// }

/**
 * 猜你喜欢组合式函数
 */
const { guessRef, onScrolltolower } = useGuessList()

/**
 * 是否触发刷新，当前下拉刷新的状态
 */
const isTriggered = ref(false)
/**
 * 自定义下拉刷新被触发
 */
const onRefresherrefresh = async () => {
  // 开启刷新动画
  isTriggered.value = true
  // 重新加载数据
  // await getHomeBannerData()
  // await getHomeCategoryData()
  // await getHomeHotData()
  // 重置猜你喜欢组件的数据
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭刷新动画
  isTriggered.value = false
}
</script>

<template>
  <!-- 自定义首页导航栏 -->
  <CustomNavbar></CustomNavbar>
  <!-- 滚动容器 -->
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 自定义首页轮播图 -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 自定义首页分类面板 -->
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <!-- 自定义首页热门推荐组件 -->
      <HotPanel :list="hotList"></HotPanel>
      <!-- 自定义首页猜你喜欢组件 -->
      <XtxGuess ref="guessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f1f1f1;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
